﻿using System.Net.Mail;
using System.Text.RegularExpressions;
using System;
using System.Configuration;
using System.Data;
using System.Web;

/// <summary>
/// Summary description for SendMailClient
/// </summary>
public class SendMail
{

    #region Constant
    const int port = 25; // 587
    const string host = "mail.mekongeyes.com"; // smtp.gmail.com
    const string emailAddress = "confirmation@mekongeyes.com"; // "mekongeyes@gmail.com"
    const string password = "#me2010"; // "123456789!@#" 
    #endregion

    #region define

    private SmtpClient smtpClient;

    #endregion

    #region Constructors & Destructors
    public SendMail() : this(false) { }

    public SendMail(string displayName)
        : this(host, port, emailAddress, password, false) { }

    public SendMail(bool enableSSL)
        : this(host, port, emailAddress, password, enableSSL) { }

    public SendMail(string displayName, bool enableSSL)
        : this(host, port, emailAddress, password, enableSSL) { }

    public SendMail(string host, int port, string emailAddress, string password)
        : this(host, port, emailAddress, password, false) { }

    public SendMail(string host, int port, string emailAddress, string password, bool enableSSL)
    {
        smtpClient = new SmtpClient(host, port);
        smtpClient.Credentials = new System.Net.NetworkCredential(emailAddress, password);
        smtpClient.EnableSsl = enableSSL;
    }
    #endregion

    #region Methods
    public int Send(string displayName, string emailTo, string subject, string body)
    {
        return Send(displayName, emailTo, subject, body, true, null, null);
    }

    public int Send(string displayName, string emailTo, string subject, string body, bool isHTMLFormat)
    {
        return Send(displayName, emailTo, subject, body, isHTMLFormat, null, null);
    }

    public int Send(string displayName, string emailTo, string subject, string body, string[] emailCC)
    {
        return Send(displayName, emailTo, subject, body, true, emailCC, null);
    }

    public int Send(string displayName, string emailTo, string subject, string body, bool isHTMLFormat, string[] emailCC)
    {
        return Send(displayName, emailTo, subject, body, isHTMLFormat, emailCC, null);
    }

    public int Send(string displayName, string emailTo, string subject, string body, string[] emailCC, string[] emailBcc)
    {
        return Send(displayName, emailTo, subject, body, true, emailCC, emailBcc);
    }

    /// <summary>
    /// Send Email
    /// </summary>
    /// <param name="emailTo"></param>
    /// <param name="subject"></param>
    /// <param name="body"></param>
    /// <param name="isHTMLFormat"></param>
    /// <returns></returns>
    public int Send(string displayName, string emailTo, string subject, string body, bool isHTMLFormat, string[] emailCC, string[] emailBcc)
    {
        try
        {
            // Validate Email
            Regex reg = new Regex(@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$");

            if (reg.IsMatch(emailTo))
            {
                MailAddress mailFrom = new MailAddress(emailAddress, displayName, System.Text.Encoding.UTF8);
                MailAddress mailTo = new MailAddress(emailTo);

                MailMessage mailMessage = new MailMessage(mailFrom, mailTo);
                mailMessage.ReplyTo = mailMessage.From;

                // CC mails
                if (emailCC != null && emailCC.Length > 0)
                {
                    foreach (var item in emailCC)
                        mailMessage.CC.Add(new MailAddress(item));
                }

                // Bcc mails
                if (emailBcc != null && emailBcc.Length > 0)
                {
                    foreach (var item in emailBcc)
                        mailMessage.Bcc.Add(new MailAddress(item));
                }

                mailMessage.Subject = subject;
                mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
                mailMessage.IsBodyHtml = isHTMLFormat;

                // specify the priority of the mail message
                //mailMessage.Priority = MailPriority.High;
                // This method has been deprecated.
                // mail.Headers.Add("Return-Receipt-To", "prashantmx@xyz.com"); 
                // Use this if you need an delivery notification of an email.             
                // DeliveryNotificationOption is an enumeration
                // and can be used to set the delivery notification on the following options:
                // 1. Delay
                // 2. Never
                // 3. None
                // 4. OnFailure
                // 5. OnSuccess
                // You can use also use OnFailure enum with OnSuccess enum. If in case the e-mail
                // fails to delivered you'll get notification for both the cases
                //mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
                // Add "Disposition-Notification-To" for Read receipt
                //mailMessage.Headers.Add("Disposition-Notification-To", "");

                string filename = "Images/logo.jpg";
                string filePath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath.Replace("/", "\\")) + "\\" + filename;

                //Creating LinkedSource for embedded picture
                LinkedResource myPhoto = new LinkedResource(filePath);
                myPhoto.ContentId = "logo";

                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(mailMessage.IsBodyHtml ? String.Format("<html><head><title>{0}</title></head><body>{1}</body></html>", subject, body) : body, null, "text/html");

                //Adding LinkedSource to AlternateView
                htmlView.LinkedResources.Add(myPhoto);

                mailMessage.AlternateViews.Add(htmlView);

                //mailMessage.Body = mailMessage.IsBodyHtml ? String.Format("<html><head><title>{0}</title></head><body>{1}</body></html>", subject, body) : body;

                smtpClient.Send(mailMessage);

                return 1;
            }
            else
            {
                return 0;
            }
        }
        catch
        {
            return -1;
        }
    } 
    #endregion

}